package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.entity.po.User;
import com.mf.mapper.UserMapper;

import com.mf.service.IUserService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户表 服务实现类
 *
 * @author mf
 * @since 2024-07-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 根据用户名查询用户
     */
    @Override
    @Transactional
    public long getUserByUserName(String userName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, userName);
        return userMapper.selectCount(queryWrapper);
    }
    /**
     * 编辑
     */
    @Override
    @Transactional
    public void edit(User user) {
        User user1 = new User();
        user1.setId(user.getId());

        user1.setName(user.getName());
        user1.setPassword(user.getPassword());
        user1.setGender(user.getGender());

        user1.setPhone(user.getPhone());
        user1.setEmail(user.getEmail());

        user1.setSystemCode(user.getSystemCode());
        userMapper.updateById(user1);
    }

    /**
     * 修改状态
     */
    @Override
    @Transactional
    public void updateStatus(Integer id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        userMapper.updateById(user);
    }

    /**
     * 验证是否有对应账号  (后面再来加上密码盐)
     */
    @Override
    @Transactional
    public long getUserByUserNameAndPassword(String userName, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, userName);
        queryWrapper.eq(User::getPassword, password);
        return userMapper.selectCount(queryWrapper);
    }
}
